package com.mucas;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CourseStack {
	
	private NodeList courseNodes	= null;
	private NodeList titleNodes		= null;
	private NodeList textNodes      = null;
	private Node courseNode			= null;
	private Element courseElement	= null;
	private Element titleElement	= null;
	private Course tmpCourse        = null;
	private String title            = null;
	
	private List<Course> stack 		= new ArrayList<Course>();
	
	public CourseStack() {
		
	}
	
	public void generateStack(Document catalogue) {
		this.courseNodes = catalogue.getElementsByTagName("course");
		
		for(int i = 0; i < this.courseNodes.getLength(); i++) {
			
			this.courseNode = this.courseNodes.item(i);
			
			if(this.courseNode.getNodeType() == Node.ELEMENT_NODE) {
				
				this.tmpCourse     = new Course();
				this.courseElement = (Element) this.courseNode;
				
				this.titleNodes    = (this.courseElement).getElementsByTagName("title");
				this.titleElement  = (Element) this.titleNodes.item(0);
				this.textNodes 	   = ((Node) this.titleElement).getChildNodes();
				this.title		   = ((Node) textNodes.item(0)).getNodeValue();
				this.tmpCourse.setTitle(this.title);
				
				NodeList descNodes   = (this.courseElement).getElementsByTagName("description");
				Element descElement  = (Element) descNodes.item(0);
				NodeList descNode 	 = ((Node) descElement).getChildNodes();
				this.tmpCourse.setDesc(((Node) descNode.item(0)).getNodeValue());
						
				this.titleNodes    = (this.courseElement).getElementsByTagName("type");
				this.titleElement  = (Element) this.titleNodes.item(0);
				this.textNodes 	   = ((Node) this.titleElement).getChildNodes();
				this.tmpCourse.setType(((Node) textNodes.item(0)).getNodeValue());
				
				this.titleNodes    = (this.courseElement).getElementsByTagName("studyMode");
				this.titleElement  = (Element) this.titleNodes.item(0);
				this.textNodes 	   = ((Node) this.titleElement).getChildNodes();
				this.tmpCourse.setMode(((Node) textNodes.item(0)).getNodeValue());
				
				this.titleNodes    = (this.courseElement).getElementsByTagName("url");
				this.titleElement  = (Element) this.titleNodes.item(0);
				this.textNodes 	   = ((Node) this.titleElement).getChildNodes();
				this.tmpCourse.setURL(((Node) textNodes.item(0)).getNodeValue());
				
				if(this.title != null)
					this.stack.add(this.tmpCourse);
				
			}
		}
	}

	public int getStackSize() {
		return this.stack.size();
	}
	
	public Course getCoursePos(int loc) {
		return this.stack.get(loc);
	}
	
	public List<Course> getStack() {
		return this.stack;
	}
	
	public void addCourse(Course module) {
		this.stack.add(module);
	}
	
	public Course findFromString(String text) {
		for(int i = 0; i < this.getStackSize(); i++) {
			if(this.getCoursePos(i).getTitle().compareTo(text) == 0) {
				return this.getCoursePos(i);
			}
		}
		return null;
	}
	
}
